package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Role;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IRoleService;
import com.woniu.supermarket.vo.RolePerVo;
import com.woniu.supermarket.vo.RoleVo;
import com.woniu.supermarket.vo.WarehousingVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("role")
@Api(value = "对角色表进行操作")
public class RoleController {
    @Autowired
    IRoleService roleService;
    //初始化角色
    @GetMapping("/rolelist")
    public ResultResponse<List<Role>> selectAllRoles(){
        ResultResponse<List<Role>> result = null;
        try {
            List<Role> roles = roleService.queryAllRoles();
            result = new ResultResponse<>(200,"查询成功！",roles);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败！");
        }
        return result ;
    }
    //分页查询所有角色
    @GetMapping("/rlist")
    @ApiOperation(value = "分页查询角色数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页",dataType = "int",paramType = "query",name = "now"),
            @ApiImplicitParam(value = "分页单位",dataType = "int",paramType = "query",name = "size"),
            @ApiImplicitParam(value = "按照名称条件查询",dataType = "String",paramType = "query",name = "roleName"),
    })
    public ResultResponse<PageInfo<RoleVo>> list(@RequestParam(value="now",defaultValue ="1")Integer now,
                                               @RequestParam(value ="size",defaultValue ="3")Integer size,
                                               String roleName){
        ResultResponse<PageInfo<RoleVo>> result = null;
        try {
            PageInfo<RoleVo> pageInfo = roleService.queryByPage(now, size, roleName);
            if(pageInfo != null){
                result = new ResultResponse<>(200,"查询角色成功",pageInfo);
            }else{
                result = new ResultResponse<>(201,"查询角色失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }
    @PutMapping("/modify")
    @ApiOperation(value = "修改角色数据")
    public ResultResponse<Void> modify(@RequestBody Role role){
        ResultResponse<Void> result = null;
        try {
            Role r = roleService.queryRoleByKey(role.getId());
            r.setRoleName(role.getRoleName());
            int i = roleService.modify(r);
            result = new ResultResponse<>(200,"修改角色成功！");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改角色失败！");
        }
        return result ;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加角色数据")
    public ResultResponse<Void> add(@RequestBody Role role){
        ResultResponse<Void> result = null;
        try {
            int i = roleService.add(role);
            result = new ResultResponse<>(200,"添加角色成功！");

        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改角色失败！");
        }
        return result ;
    }

    @DeleteMapping("/del/{id}")
    @ApiImplicitParam(value = "删除角色 根据id删除",dataType = "int",name = "id",paramType = "path")
    public ResultResponse<Void> del(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = roleService.remove(id);
            result = new ResultResponse<>(200,"删除角色成功！");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除角色失败！");
        }
        return result ;
    }

    @GetMapping("/getCheckedPerIds/{id}")
    @ApiOperation(value = "查询角色拥有权限用的方法")
    public ResultResponse<List<Long>> getCheckedPerIds(@ApiParam(value = "查询权限的id") @PathVariable  Long id){
        ResultResponse<List<Long>> result = null;
        List<Long> list = roleService.queryPerIdsByRoleKey(id);
        try {
            if(list != null){
                result = new ResultResponse<>(200,"查询角色权限成功",list);
            }else{
                result = new ResultResponse<>(201,"查询角色权限失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }

    @PostMapping("/roleper")
    public ResultResponse<Void> rolePer(@RequestBody RolePerVo rolePerVo){
        ResultResponse<Void> result = null;
        int i = roleService.rolePer(rolePerVo);
        try {
            if(i >0){
                result = new ResultResponse<>(200,"授权成功");
            }else{
                result = new ResultResponse<>(201,"授权失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }

}
